package com.chinatgl.latte.ec.sign;


import com.chinatgl.latte.app.AccountManager;
import com.chinatgl.latte.app.IUserChecker;
import com.chinatgl.latte.delegates.LatteDelegate;
import com.chinatgl.latte.ec.database.DatabaseManager;
import com.chinatgl.latte.ec.database.UserProfile;
import com.chinatgl.latte.util.json.JsonUtil;
import com.chinatgl.latte.util.json.Result;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.greendao.query.QueryBuilder;

/**
 * 文件描述:
 * 作者: Created by 杨伟乔 on 2017/8/9 0009.
 * 版本号: 1.0
 */

public class SignHandler {

    public static void onSignUp(String response, ISignListener iSignListener) {

        final Result<UserProfile> result = JsonUtil.parseJsonToBean(response, new TypeToken<Result<UserProfile>>() {
        }.getType());
        DatabaseManager.getInstance().getDao().insert(result.getResult());
        //保存用户的登录状态
        AccountManager.setSignState(true);
        iSignListener.onSignUpSuccess();
    }


    public static void onSignIn(String response, ISignListener iSignListener) {


        final Result<UserProfile> result = JsonUtil.parseJsonToBean(response, new TypeToken<Result<UserProfile>>() {
        }.getType());
        DatabaseManager.getInstance().getDao().insert(result.getResult());
        //保存用户的登录状态
        AccountManager.setSignState(true);
        iSignListener.onSignInSuccess();

    }

    /**
     * 用户用户信息
     *
     * @return 返回用户对象
     */
    public static UserProfile getUserProfile() {
        final QueryBuilder<UserProfile> userProfileQueryBuilder = DatabaseManager.getInstance().getDao().queryBuilder();
        final UserProfile userProfile = userProfileQueryBuilder.build().unique();
        return userProfile;
    }
}
